package com.example.android.BluetoothChat.service.impl;

import java.util.ArrayList;
import java.util.List;

import android.app.Service;
import android.content.Intent;
import android.util.Log;

import com.example.android.BluetoothChat.activity.MGDPreferencessActivity;
import com.example.android.BluetoothChat.activity.PlayerListActivity;
import com.example.android.BluetoothChat.bean.Game;
import com.example.android.BluetoothChat.bean.Player;
import com.example.android.BluetoothChat.listener.NewGameRequestListener;
import com.example.android.BluetoothChat.util.factory.ComponentFactory;

public abstract class ADiscoveryService extends Service {

	private final List<NewGameRequestListener> _newGameRequestListenerList = new ArrayList<NewGameRequestListener>();

	private boolean _started = false;

	protected void fireRequestForPlaying(Player parPlayer, Game parGame) {
		for(NewGameRequestListener locListener : _newGameRequestListenerList){
			locListener.handleRequestForPlaying(parPlayer, parGame);
		}		
		ComponentFactory.createNewGameRequestNotification(getApplicationContext(), parPlayer, parGame);
	}	

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		if(PlayerListActivity.D) Log.i(getClass().getSimpleName(), "starting discovery service");
		_started = true;
		// We want this service to continue running until it is explicitly
		// stopped, so return sticky.
		return START_STICKY;
	}

	@Override
	public boolean stopService(Intent service){
		if(PlayerListActivity.D) Log.i(getClass().getSimpleName(), "stopping discovery service");
		_started = false;
		return true;
	}
	
	public void addNewGameRequestListener(NewGameRequestListener parListener) {
		_newGameRequestListenerList.add(parListener);
	}
	

	public void removeNewGameRequestListener(NewGameRequestListener parListener) {
		_newGameRequestListenerList.remove(parListener);
	}
	
	protected boolean isStarted(){
		return _started;
	}
}
